package com.yonyou.gmmc.service.pointactivity.service;



import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yonyou.gmmc.service.pointactivity.dao.ReservationMapper;
import com.yonyou.gmmc.service.pointactivity.dao.TtOwnerLotteryMapper;
import com.yonyou.gmmc.service.pointactivity.model.TmReturnFactory;
import com.yonyou.gmmc.service.pointactivity.model.TmSweepstake;
import com.yonyou.gmmc.service.pointactivity.model.TtReturnFactoryReserve;
import com.yonyou.gmmc.service.pointactivity.model.TcSysConfig;
import com.yonyou.gmmc.service.pointactivity.model.TmDealer;
import com.yonyou.gmmc.service.pointactivity.model.TmRegion;

/**
 * 省市县地区查询服务
 * @author baodu
 *
 */
@Service("reservationService")
@Transactional
public class ReservationService {

	@Autowired
	private ReservationMapper reservationMapper;
	@Autowired
	private TtOwnerLotteryMapper lotteryMapper;
	
	private static final String key="10000001";   //一年未返厂活动开始时间key
	private static final String key2="10000002";   //一年未返厂活动结束时间key
	private static final String parentCode="0";   //定义省份查询编码
	/**
	 * 根据id查询省市区
	 * @return
	 */
	public Map<String, Object> getProvince() {
		Map<String, Object>map=new HashMap<String,Object>();
		List<TmRegion>list=reservationMapper.selectProvince(parentCode);
		for(TmRegion region:list){
			map.put(region.getRegionCode(), region.getRegionName());
		}
		return map;
	}


	/**
	 * 根据省份id查询下级所有城市
	 * @param regionCode 
	 * @return
	 */
	public Map<String, Object> getCity(String regionCode) {
		Map<String, Object>map=new HashMap<String, Object>();
		List<TmRegion>list=reservationMapper.selectProvince(regionCode);
		for(TmRegion region:list){
			map.put(region.getRegionCode(), region.getRegionName());
		}
		return map;
	}

	
	/**
	 * 根据城市编码查询当地经销商
	 * @param regionCode
	 * @return
	 */
	public Map<String, Object> getDealer(String regionCode) {
		Map<String, Object>map=new HashMap<String, Object>();
		List<TmDealer>list=reservationMapper.selectDealer(regionCode);
		if (list.size()==0) {
			map.put("message", "该城市目前未开通经销商服务！");
		}else{
			for(TmDealer dealer:list){
				map.put(dealer.getDealerCode(), dealer.getDealerShortName());
			}
		}
		return map;
	}

	
	/**
	 * 保存信息
	 * @param reserve
	 */
	public Integer saveReserve(TtReturnFactoryReserve reserve) {
		String vin = lotteryMapper.getVinByOpenId(reserve.getOpenId());
		reserve.setVin(vin);
		reservationMapper.save(reserve);
		Integer reserveId = reserve.getReserveId();
		if (reserveId > 0) {
			lotteryMapper.reservationStatusUpdate(reserve.getOpenId());
			return reserveId;
		} else {
			return 0;
		}
	}

	/**
	 * 查询活动时间
	 * @return
	 */
	public Map<String, Object> getTime() {
		Map<String, Object>map=new HashMap<String, Object>();
		List<String>keys=new ArrayList<String>();
		keys.add(key);
		keys.add(key2);
		List<TcSysConfig>list=reservationMapper.selectTime(keys);
		//String s1 = "2003-12-12";
		//String s2 = "2004-04-01";
		//int res = s1.compareTo(s2);
		//这个时候res<0，所以s1<s2
		
		//获取系统当前时间
		SimpleDateFormat sdf =   new SimpleDateFormat("yyyy-MM-dd");
		String date=sdf.format(new Date()); 
		for(TcSysConfig config:list){
			//比较两者时间
			String conf=config.getSysConfigValue();
			int res=conf.compareTo(date);
			if(res<0){
				map.put(config.getSysonfigKey(), date);
			}else if(res>0){
				map.put(config.getSysonfigKey(), conf);
			}else{
				map.put(config.getSysonfigKey(), conf);
			}
		}
		Map<String, Object>resultmap=new HashMap<String, Object>();
		resultmap.put("min", map.get("10000001"));
		resultmap.put("max", map.get("10000002"));
		return resultmap;
	}

	
	/**
	 * 初始化奖券数据
	 */
	public void sweepstakeInit() {
		List<TmReturnFactory>list=reservationMapper.getProvinceAndVin(); 
		TmSweepstake sweepstake=null;
		if (list.size()>0) {	
			for(TmReturnFactory factory:list){
				if (factory.getFactoryProvinceName().equals("其它地区")) {
					int sum=factory.getLotteries()+2;
					for(int i=0;i<sum;i++){
						sweepstake=new TmSweepstake();
						sweepstake.setCreateDate(new Date());
						sweepstake.setTmSweepstakeProvince(factory.getFactoryProvinceName());
						sweepstake.setTmSweepstakeState("2");
						sweepstake.setUpdateDate(new Date());
						reservationMapper.allotLotteries(sweepstake);
					}
				}else {
					for(int i=0;i<factory.getLotteries();i++){
						sweepstake=new TmSweepstake();
						sweepstake.setCreateDate(new Date());
						sweepstake.setTmSweepstakeProvince(factory.getFactoryProvinceName());
						sweepstake.setTmSweepstakeState("2");
						sweepstake.setUpdateDate(new Date());
						reservationMapper.allotLotteries(sweepstake);
					}
				}
			}
		}
	}

	/**
	 * 根据openId查询是否已经预约
	 * @param openId
	 * @return
	 */
	public String findByOpenId(String openId) {
		String oId=null;
		if(openId!=null){
			List<TtReturnFactoryReserve>list=reservationMapper.getByopenId(openId);
			if (list.size()>0) {
				oId=list.get(0).getOpenId();
			}
		}
		return oId;
	}

	/**
	 * 根据省份名称查询是否存在
	 * @param province
	 * @return
	 */
	public TmRegion checkOutProvince(String province) {
		TmRegion region=null;
		if(province!=null){
			List<TmRegion>list=reservationMapper.selectByProvince(province);
			region=list.get(0);
		}
		return region;
	}

	/**
	 * 根据城市名称查询是否存在
	 * @param dealerCode
	 * @return
	 */
	public TmRegion checkOutCity(String city) {
		TmRegion region=null;
		if (city!=null) {
		   List<TmRegion>list=reservationMapper.selectByCity(city);
		   region=list.get(0);
		}
		return region;
	}
	
	/**
	 * 根据经销商名称查询是否存在
	 */
	public TmDealer chenkOutDealer(String dealerCode){
		TmDealer dealer=null;
		if(dealerCode!=null){
			List<TmDealer> list =reservationMapper.selectByDealer(dealerCode);
			dealer=list.get(0);
		}
		return dealer;
	}
	
	/**
	 * 根据openId查询车架号
	 */
	public String findByVin(String openId){
		String vin=null;
		if(openId!=null){
	      vin = lotteryMapper.getVinByOpenId(openId);
	      if(vin!=null){
	    	  return vin;
	      }
		}
		return vin;
	}

	
	/**
	 * 根据openId查询用户车辆所在地省份
	 * @param openId
	 * @return
	 */
	public String getCarAndProvince(String openId) {
		String province=null;
		if(openId!=null || !("".equals(openId))){
			province=reservationMapper.findByProvince(openId);
		}
		return province;
	}
	
}
